<template>
    <div class="refund-saveRefund">
        <el-button type="success" @click="_show" icon="el-icon-upload2">{{$t('m.button.createARefund')}}</el-button>
        <el-dialog :title="$t('m.button.createARefund')" :visible.sync="dialogFormVisible" width="480px">
            <el-form :model="saveRefund" ref="saveRefund" :rules="rules" label-width="80px">
                <el-form-item :label="$t('m.label.account')">
                    <el-select v-model="saveRefund.sellerId" :placeholder="$t('m.placeholder.selectAccount')">
                        <el-option
                            v-for="item in findAccount"
                            :key="item.sellerId"
                            :label="item.sellerName"
                            :value="item.sellerId"
                            disabled="disabled"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item  :label="$t('m.label.country')">
                    <el-select v-model="saveRefund.countryId" :placeholder="$t('m.placeholder.selectCountry')">
                        <el-option
                            v-for="item in findCountry"
                            :key="item.countryId"
                            :label="item.countryName"
                            :value="item.countryId"
                            disabled="disabled"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item :label="$t('m.label.orderNumber')" prop="amzOrderId">
                    <el-input
                        v-model="saveRefund.amzOrderId"
                        @change="getfindRefund"
                        :placeholder="$t('m.placeholder.pleaseEnteOrderNumber')"
                        style="width: 200px;"
                    ></el-input>
                </el-form-item>
                <div v-for="(item,index) in findRefund" :key="index">
                    {{$t('m.label.productModelNumber')}}：{{item.productModelNumber}}
                    <el-form-item :label="$t('m.label.refundAmount')" style="display: inline-block;">
                        <el-input-number
                            v-model="item.amzRefundAmount"
                            :precision="2"
                            :step="0.1"
                            controls-position="right"
                            size="small"
                            :max="item.maxRefund"
                        ></el-input-number>
                    </el-form-item>
                    {{$t('m.label.amount')}}：{{item.maxRefund}}
                </div>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">{{$t('m.button.cancel')}}</el-button>
                <el-button type="primary" @click="getSaveRefund('saveRefund')">{{$t('m.button.determine')}}</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
const ERR_OK = 200;
export default {
    data() {
        var validatePass = (rule, value, callback) => {
            if (this.saveRefund.amzOrderId === "") {
                callback(new Error(this.$t('m.placeholder.pleaseEnteOrderNumber')));
            } else {
                if (this.findRefund.length > 0) {
                    callback();
                    return;
                }
                callback(new Error(this.$t('m.placeholder.orderNumberMistaken')));
            }
        };
        return {
            dialogFormVisible: false,
            findCountry: [],
            findAccount: [],
            findRefund: [],
            saveRefund: {
                amzOrderId: ""
            },
            rules: {
                amzOrderId: [{ validator: validatePass, trigger: "blur" }]
            }
        };
    },
    methods: {
        //获取账号
        getfindAccount() {
            this.$ajax
                .post(
                    this.GLOBAL.reviewMonitoringService +
                        "/tracking/findAccount"
                )
                .then(res => {
                    if (res.data.code === ERR_OK) {
                        this.findAccount = res.data.data.data;
                    }
                });
        },
        //获取国家
        getFindCountry() {
            this.$ajax
                .post(
                    this.GLOBAL.reviewMonitoringService +
                        "/reviewSearch/findCountry"
                )
                .then(res => {
                    if (res.data.code === ERR_OK) {
                        this.findCountry = res.data.data.data;
                    }
                });
        },
        //获取订单号信息
        getfindRefund() {
            this.findRefund = [];
            this.$ajax
                .post(
                    this.GLOBAL.reviewMonitoringService +
                        "/tracking/findRefund?orderId=" +
                        this.saveRefund.amzOrderId
                )
                .then(res => {
                    if (res.data.code === ERR_OK) {
                        if (!res.data.data.error) {
                            if (res.data.data.data.length > 0) {
                                this.findRefund = res.data.data.data;
                                this.saveRefund.sellerId = this.findRefund[0].sellerId;
                                this.saveRefund.countryId = this.findRefund[0].countryId;
                                this.$refs["saveRefund"].validate(
                                    valid => true
                                );
                            }
                        }
                    }
                });
        },
        // 创建退款
        getSaveRefund(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    let dataArr = [];
                    this.findRefund.forEach(el => {
                        dataArr.push({
                            amzOrderId: this.saveRefund.amzOrderId,
                            amzRefundAmount: el.amzRefundAmount,
                            asinId: el.asinId,
                            countryId: el.countryId,
                            sellerId: el.sellerId
                        });
                    });
                    this.$ajax({
                        method: "post",
                        url:
                            this.GLOBAL.reviewMonitoringService +
                            "/tracking/saveRefund?userId=" +
                            this.$store.getters.userId,
                        data: dataArr
                    }).then(res => {
                        if (res.data.code === ERR_OK) {
                            this.dialogFormVisible = false;
                            this.$message.success(res.data.data.data);
                            this.$emit("getfindDeta");
                        }
                    });
                } else {
                    return false;
                }
            });
        },
        _show() {
            this.dialogFormVisible = true;
            if (
                this.findCountry.length === 0 ||
                this.findAccount.length === 0
            ) {
                this.getFindCountry();
                this.getfindAccount();
            }
        }
    }
};
</script>

<style lang="stylus" scoped>
.refund-saveRefund {
    display: inline-block;
}
</style>
